package com.company.ems.util;

import com.company.ems.dao.EmployeeDAO;
import com.company.ems.dao.EmployeeDAOImpl;
import com.company.ems.model.Employee;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

public class TestEmployeeCRUD {
    
    public static void main(String[] args) {
        EmployeeDAO employeeDAO = new EmployeeDAOImpl();
        
        try {
            System.out.println("=== 测试员工信息管理功能 ===\n");
            
            // 1. 测试查询所有员工
            System.out.println("1. 查询所有员工:");
            List<Employee> employees = employeeDAO.findAll();
            for (Employee emp : employees) {
                System.out.println("   ID: " + emp.getId() + ", 姓名: " + emp.getName() + 
                    ", 部门: " + emp.getDepartment() + ", 职位: " + emp.getPosition());
            }
            
            if (!employees.isEmpty()) {
                // 2. 测试根据ID查询员工
                System.out.println("\n2. 查询ID为1的员工:");
                Employee emp = employeeDAO.findById(1);
                if (emp != null) {
                    System.out.println("   姓名: " + emp.getName() + ", 部门: " + emp.getDepartment() + 
                        ", 职位: " + emp.getPosition() + ", 薪资: " + emp.getSalary());
                }
                
                // 3. 测试添加员工
                System.out.println("\n3. 添加新员工:");
                Employee newEmployee = new Employee();
                newEmployee.setName("测试员工");
                newEmployee.setDepartment("技术部");
                newEmployee.setPosition("测试工程师");
                newEmployee.setSalary(new BigDecimal("10000.00"));
                newEmployee.setEmail("test@example.com");
                newEmployee.setPhone("13800138000");
                newEmployee.setHireDate(new SimpleDateFormat("yyyy-MM-dd").parse("2023-01-01"));
                newEmployee.setCreatedBy(1); // admin用户ID
                
                boolean added = employeeDAO.add(newEmployee);
                System.out.println("   添加结果: " + (added ? "成功" : "失败"));
                
                // 4. 测试更新员工
                if (added && !employeeDAO.findAll().isEmpty()) {
                    System.out.println("\n4. 更新员工信息:");
                    Employee latestEmployee = employeeDAO.findAll().get(0);
                    latestEmployee.setName("更新后的测试员工");
                    latestEmployee.setSalary(new BigDecimal("12000.00"));
                    
                    boolean updated = employeeDAO.update(latestEmployee);
                    System.out.println("   更新结果: " + (updated ? "成功" : "失败"));
                }
                
                // 5. 测试删除员工
                if (added && !employeeDAO.findAll().isEmpty()) {
                    System.out.println("\n5. 删除员工:");
                    Employee employeeToDelete = employeeDAO.findAll().get(0);
                    boolean deleted = employeeDAO.delete(employeeToDelete.getId());
                    System.out.println("   删除结果: " + (deleted ? "成功" : "失败"));
                }
            }
            
            // 6. 测试条件查询
            System.out.println("\n6. 条件查询:");
            List<Employee> searchResults = employeeDAO.findByCondition("张", "技术部", null);
            System.out.println("   搜索姓名包含'张'且部门为'技术部'的员工:");
            for (Employee emp : searchResults) {
                System.out.println("   ID: " + emp.getId() + ", 姓名: " + emp.getName());
            }
            
            System.out.println("\n=== 所有功能测试完成 ===");
            
        } catch (Exception e) {
            System.err.println("测试过程中出现异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
}